#include "kit/event/BroadcastEvent.hpp"
#include "kit/container/ThreadSafeMap.hpp"
#include <string>
#include <vector>
#include "kit/tool/Tokenizer.hpp"

namespace KitEvent
{
	BroadcastEvent::~BroadcastEvent()
	{

	}

	void BroadcastEvent::execute(KitUser::UserMap * iUsers)
	{
		std::vector<std::string> aKeys = iUsers->keys();
		// for every user that is present send him a message
		for(int i = 0; i < aKeys.size(); ++i)
		{
			KitUser::User * aUser = iUsers->get(aKeys[i]);
			try
			{
				if(aUser->getConnection()->isValid())
					aUser->getConnection()->write(_message->getPayload());
			}
			catch(SystemSocket::WriteException & aException)
			{
				// if we couldn't write anymore on the connection mark it as invalid
				aUser->getConnection()->markInvalid();
			}
		}
	}

	bool BroadcastEvent::isValid(KitUser::UserMap * iUsers)
	{
		// broadcast event is always valid ;)
		return true;
	}

}
